class Solution {
public:
    string ReverseSentence(string str) {
        if(str.empty())
			return str;
		int length = str.size();
		reverse(0,length-1,str);
		int begin = 0;
		for(int idx = 0 ; idx < length ; ++idx)
		{
			if(str[idx] == ' ')
			{
				reverse(begin,idx-1,str);
				begin = idx+1;
			}
		}
		reverse(begin,length-1,str);
		return str;
    }

    void reverse(int i,int j,string &arr)
	{
		while(i<j)
		{
			char temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
			++i;
			--j;
		}
	}
};